package com.example.partner.activity.team;

import static com.example.partner.common.ConfigConstant.BASE_URL;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.partner.R;
import com.example.partner.common.BaseResponse;
import com.example.partner.model.vo.TeamUserVo;
import com.example.partner.adapter.team.TeamAdapter;
import com.example.partner.service.TeamService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class JoinTeamActivity extends AppCompatActivity {

    private static final String TAG = "JoinTeamActivity";
    ListView listView;
    private ImageView back;

    TeamAdapter adapter;

    @Override
    public SharedPreferences getSharedPreferences(String name, int mode) {
        return super.getSharedPreferences(name, mode);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join_team);
        listView = findViewById(R.id.joinTeamListView);
        back = findViewById(R.id.back);
        SharedPreferences sharedPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
        Long userId = sharedPreferences.getLong("userId", -1);
        listMyJoinTeams(userId);
        back.setOnClickListener(view -> {
            // 处理点击事件
            finish(); // 关闭当前 Activity，返回上一页
        });

    }

    private void listMyJoinTeams(Long userId) {


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        TeamService service = retrofit.create(TeamService.class);
        Call<BaseResponse<List<TeamUserVo>>> call = service.listMyJoinTeams(userId);

        call.enqueue(new Callback<BaseResponse<List<TeamUserVo>>>() {
            @Override
            public void onResponse(Call<BaseResponse<List<TeamUserVo>>> call, Response<BaseResponse<List<TeamUserVo>>> response) {
                if (response.isSuccessful()) {
                    List<TeamUserVo> teamUserVoList = response.body().getData();
                    if (teamUserVoList != null){
                        adapter = new TeamAdapter(JoinTeamActivity.this, teamUserVoList);
                        listView.setAdapter(adapter);
                        adapter.setOnItemMoreInfoClickListener(new TeamAdapter.onItemMoreInfoListener() {
                            @Override
                            public void onMoreInfoClick(int i) {//重写接口里的方法
                                // 获取点击的项
                                TeamUserVo selectedTeam = (TeamUserVo) adapter.getItem(i);

                                // 创建Intent并启动新的Activity
                                Intent intent = new Intent(JoinTeamActivity.this, TeamInfoActivity.class);
                                intent.putExtra("descript",selectedTeam.getDescription());
                                intent.putExtra("img",selectedTeam.getAvatarUrl());
                                intent.putExtra("teamname", selectedTeam.getName());
                                intent.putExtra("teamid",selectedTeam.getId());
                                intent.putExtra("announce",selectedTeam.getAnnounce());
                                intent.putExtra("status",selectedTeam.getStatus());
                                startActivity(intent);
                            }
                        });
                    }
                    else
                        Toast.makeText(JoinTeamActivity.this, "没有加入的队伍", Toast.LENGTH_SHORT).show();

                } else {
                    Log.e(TAG, "请求错误: " + response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseResponse<List<TeamUserVo>>> call, Throwable t) {
                Log.e(TAG, "网络错误: " + t.getMessage());
            }
        });
    }


}